package com.qidi.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import com.qidi.ssh.bean.Customer;
import com.qidi.ssh.bean.Linkman;
import com.qidi.ssh.service.CustomerService;
import com.qidi.ssh.service.LinkmanService;
import com.qidi.ssh.utils.PageBean;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-10-20-9:05
 */
public class LinkmanAction extends ActionSupport implements ModelDriven<Linkman> {

    private LinkmanService linkmanService;
    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setLinkmanService(LinkmanService linkmanService) {
        this.linkmanService = linkmanService;
    }
    private Linkman linkman=new Linkman();

    @Override
    public Linkman getModel() {
        return linkman;
    }
    private Integer pageNo=1;
    private Integer pageSize=3;


    public void setPageNo(Integer pageNo) {
        if (pageNo==null){
            pageNo=1;
        }
        this.pageNo = pageNo;
    }


    public void setPageSize(Integer pageSize) {
        if (pageSize==null){
            pageSize=3;
        }
        this.pageSize = pageSize;
    }
    public String list(){
        DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Linkman.class);
        if (linkman.getLkm_name()!=null){
            detachedCriteria.add(Restrictions.like("lkm_name","%"+linkman.getLkm_name()+"%"));
        }
        if (linkman.getLkm_gender()!=null && !"".equals(linkman.getLkm_gender())){
            detachedCriteria.add(Restrictions.eq("lkm_gender",linkman.getLkm_gender()));
        }
        PageBean<Linkman> pageBean=linkmanService.findAllByPage(detachedCriteria,pageNo,pageSize);

        ActionContext.getContext().getValueStack().push(pageBean);
        return "listSuccess";
    }
    public String addpage(){
        List<Customer> list = customerService.findAlls();
        ActionContext.getContext().getValueStack().set("list",list);
        return "addpageSuccess";
    }
    public String add(){
        linkmanService.saveLinkMan(linkman);
//        return "addSuccess";
        return NONE;
    }
    public String delete(){
        linkmanService.deleteLinkMan(linkman.getLkm_id());
        return "deleteSuccess";
    }
    public String edit(){
        List<Customer> list = customerService.findAlls();
        ServletActionContext.getContext().getValueStack().set("list",list);
        Linkman linkman = linkmanService.editById(this.linkman.getLkm_id());
        ServletActionContext.getContext().getValueStack().push(linkman);
        return "editSuccess";
    }
    public String update(){
        linkmanService.updateLinkman(linkman);
        return "updateSuccess";
    }

}
